package com.ballmaze.service;

import com.ballmaze.BeanUtil;
import com.ballmaze.maze.MazeManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

//轮询服务
@Component
@Slf4j
public class PollingService {
    private final int TURN_SECONDS = 5000;
    MazeManager mazeManager;
    WebSocketService webSocketService;

    public PollingService() {
        log.info("【PollingService】 Initialized....");
        mazeManager = BeanUtil.getBean(MazeManager.class);
        webSocketService = BeanUtil.getBean(WebSocketService.class);
        System.out.println(mazeManager);
        Runnable runnable = () -> {
            while (true) {
                if (mazeManager.getStatusController().isAlive()) {
                    //只有当状态是“活跃”的时候 才会进行发送信息 和执行 actions
                    mazeManager.executeActions();
                    webSocketService.sendMessage();
                    mazeManager.clearActionsAndRewards();
                }
                try {
                    Thread.sleep(TURN_SECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
}
